﻿using ExcelParser.CodeGen;
using ExcelParser.Reader;
using System.Collections.Generic;
using System.Text;

namespace ExcelParser.Writer
{
    public abstract class CodeWriter
    {
        protected StringBuilder m_builder = new StringBuilder();
        protected Context m_context = new Context();

        public int Indent
        {
            get { return m_context.Indent; }
            set { m_context.Indent = value; }
        }
        /// <summary>
        /// 写一行
        /// </summary>
        /// <param name="s"></param>
        /// <param name="args"></param>
        public void WriteLine(string s, params object[] args)
        {
            for (int i = 0; i < m_context.Indent; i++)
            {
                WriteIndent();
            }
            m_builder.AppendFormat(s, args);
            m_builder.AppendLine();
        }

        /// <summary>
        /// 写一行
        /// </summary>
        public void WriteLine()
        {
            m_builder.AppendLine();
        }

        /// <summary>
        /// 写头
        /// </summary>
        public virtual void WriteHeader() { }
        /// <summary>
        /// 写注释
        /// </summary>
        /// <param name="desc"></param>
        public virtual void WriteComment(string desc) { }
        /// <summary>
        /// 写缩进
        /// </summary>
        protected virtual void WriteIndent()
        {
            m_builder.Append("    ");
        }
        /// <summary>
        /// 转换类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public abstract string ConvertType(string type);
        /// <summary>
        /// 转换完整类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public abstract string ConvertFullType(string type);

        /// <summary>
        /// 生成代码
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="name"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        public abstract string GenCode(ExcelSheet sheet, string name, ICollection<ExcelField> fields);

        public abstract string GenUnityConfigManager(List<ExcelSheet> sheets, string nspace);
    }
}